package raku7.server;

import raku.packet.Packet;

public class ServerPacketReader extends Thread{
	Server server;
	ServerProgram serverProgram;
	public ServerPacketReader(ServerProgram serverProgram, Server server) {
		this.serverProgram = serverProgram;
		this.server = server;
	}
	public void run(){
		while(true){
			Message p = (Message)server.getPacket();
			if(p != null){
				switch(p.getType()){
				case Packet.MAKE_ROOM:
					serverProgram.logMessage(p.getConnection().getIP()+":MAKE_ROOM");
					serverProgram.addRoom(p.getConnection());
					break;
				case Packet.JOIN_ROOM:
					serverProgram.logMessage(p.getConnection().getIP()+":JOIN_ROOM");
					try {
						serverProgram.joinRoom(p.getIndex(), p.getConnection());
					} catch (Exception e1) {e1.printStackTrace();}
					break;
				case Packet.DISCONNECT:
					try {
						server.disconnect(p.getConnection());
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				}
			}
		}
	}
}
